package com.neiru.divider.thread;

import android.os.Handler;
import android.os.Message;

import com.neiru.divider.bean.Ball;
import com.neiru.divider.bean.Direction;
import com.neiru.divider.bean.Divider;
import com.neiru.divider.bean.Position;
import com.neiru.divider.bean.Section;
import com.neiru.divider.constants.Axis;

public class BallThread extends Thread {
	public Section getSection() {
		return section;
	}

	public void setSection(Section section) {
		this.section = section;
	}
	
	public static int instanceCounter = 0;
	
	private Section section;
	private Ball ball;
	private Divider divider;
	
	private Handler ballHandler;
	
	private boolean stop = false;
	

	public BallThread(Section section, Ball ball, Handler ballHandler) {
		super();
		this.section = section;
		this.ball = ball;
		this.ballHandler = ballHandler;
	}
	
	public void run () {
		BallThread.instanceCounter++;
		normalizeDirection();
		while(!stop) {
			updatePosition();
			try {
				Thread.sleep(5);
			} catch (InterruptedException e) {
			}
		}
	}
	private void updatePosition() {
		Position actualPosition = this.ball.getActualPosition();
		Direction actualDirection = this.ball.getActualDirection();
		Double actualX = actualPosition.getX();
		Double actualY = actualPosition.getY();
		
		
		if(actualX >= (section.getRight() - 3)) {
			if(actualDirection.getX() > 0) {
				actualDirection.setX(actualDirection.getX() * (-1));
			}
		}
		if(actualX <= (section.getLeft() + 3)) {
			if(actualDirection.getX() < 0) {
				actualDirection.setX(actualDirection.getX() * (-1));
			}
		}
		if(actualY >= (section.getBottom() - 3)) {
			if(actualDirection.getY() > 0) {
				actualDirection.setY(actualDirection.getY() * (-1));
			}
		}
		if(actualY <= (section.getTop() + 3)) {
			if(actualDirection.getY() < 0) {
				actualDirection.setY(actualDirection.getY() * (-1));
			}
		}

		if(divider != null) {
			if(!divider.isFinished() && !divider.isCanceled()) {
				Position dividerStartPosition = divider.getStartPosition();
				Position dividerEndPosition = divider.getFinalPosition();
				if(Axis.AXIS_X.equals(divider.getAxis())) {
					if(actualX > dividerStartPosition.getX() && actualX < dividerEndPosition.getX()) {
						if(actualY < dividerStartPosition.getY() + 4 && actualY > dividerStartPosition.getY() - 4) {
							divider.setCanceled(true);
							this.stop = true;
						}
					}
				} else {
					if(actualY > dividerStartPosition.getY() && actualY < dividerEndPosition.getY()) {
						if(actualX < dividerStartPosition.getX() + 4 && actualX > dividerStartPosition.getX() - 4) {
							divider.setCanceled(true);
							this.stop = true;
						}
					}
				}
			}
		}
		actualPosition.setX(actualX + ((ball.getSpeed().doubleValue()/10.0)*(actualDirection.getX())) );
		actualPosition.setY(actualY + ((ball.getSpeed().doubleValue()/10.0)*(actualDirection.getY())) );
	}
	
	private void crashWithDivider() {
		
	}
	
	private void normalizeDirection() {
		Double i = this.ball.getActualDirection().getX();
		Double j = this.ball.getActualDirection().getY();
		Double abs = Math.sqrt( Math.pow(i, 2.0) + Math.pow(j,2.0) );
		i = i / abs;
		j = j / abs;
		this.ball.getActualDirection().setX(i);
		this.ball.getActualDirection().setY(j);
	}
	public void setDivider(Divider divider) {
		this.divider = divider;
	}
	public void killAllThreads() {
		this.stop = true;
	}
}
